package com.tx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tx.entity.User;
import com.tx.mapper.UserMapper;
import com.tx.service.IUserService;
import com.tx.util.NoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public int registerUser(User user) {
        //获取用户的编号
        int no = selectMaxId();
        //构建实体
        user.setUserNo(NoUtil.getNo("U", no));
        //存入数据库
        int row = userMapper.insert(user);
        return row;
    }

    @Override
    public User selectUserByUserName(String loginName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_name", loginName);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }


    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public Integer selectMaxId() {
        Integer maxId = userMapper.selectMaxId();
        return maxId == null ? 0 :  maxId + 1;

    }

    @Override
    public Page<User> selectUserByPage(Page<User> userPage,User user) {
        if (user == null){
            user = new User();
        }
        return userMapper.selectUserByPage(userPage,user);
    }

}
